package com.xcfw.dorm.seviceimpl.dormcenter;

import com.xcfw.dorm.entity.Building;
import com.xcfw.dorm.entity.Duty;
import com.xcfw.dorm.exception.ServiceException;
import com.xcfw.dorm.mapper.BuildingMapper;
import com.xcfw.dorm.sevice.BaseService;
import com.xcfw.dorm.sevice.ZSGCService;
import com.xcfw.dorm.util.DormUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @program: Dorm
 * @description: 楼栋
 * @author: Bruse Queen
 * @create: 2018-04-07 17:52
 **/
@Service("BuildingServiceImpl")
public class BuildingServiceImpl extends BaseService implements ZSGCService {
    @Autowired
    private BuildingMapper mapper;

    @Override
    public void add(Object o) {
        Building param = (Building) o;
        param.setUuid(DormUtil.getUUID());
        zsgcStatus = mapper.insertSelective(param);
        if (1 != zsgcStatus) {
            throw new ServiceException("新增楼栋信息失败！");
        }
    }

    @Override
    public void delete(Integer id) {
        zsgcStatus = mapper.deleteByPrimaryKey(id);
        if (1 != zsgcStatus) {
            throw new ServiceException("删除楼栋信息失败！");
        }
    }

    @Override
    public void update(Object o) {
        Building param = (Building) o;

        Building temp = mapper.selectByPrimaryKey(param.getId());
        if (temp != null) {
            if (param.equals(temp)) {
                throw new ServiceException("您未修改任何信息！");
            }
        }
        zsgcStatus = mapper.updateByPrimaryKeySelective(param);
        if (1 != zsgcStatus) {
            throw new ServiceException("修改楼栋信息失败！");
        }
    }

    @Override
    public Object select(Object o) {
        List<Building> list = mapper.selectActionSql(null);
        if (list.size() == 0) {
            throw new ServiceException("暂无相关数据！");
        }
        return list;
    }

    public Object getBuildingJson(Object o) {
        Building building = (Building) o;
        List<Building> list = mapper.selectActionSql(building.getBuilding());
        if (list.size() == 0) {
            throw new ServiceException("暂无相关数据！");
        }
        Building buildingTemp = list.get(0);

        List<Object> total = new LinkedList<>();

        for (int i = buildingTemp.getBeginFloor(); i <= buildingTemp.getEndFloor(); i++) {
            Map<String, Object> map = new HashMap<>();
            List<Object> listTemp = new LinkedList<>();
            for (int j = buildingTemp.getBeginRoom(); j <= buildingTemp.getEndRoom(); j++) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("value", j);
                map1.put("label", j < 10 ? String.valueOf(i) + "0" + j : String.valueOf(i) + j);
                listTemp.add(map1);
            }
            map.put("value", i);
            map.put("label", i + "楼");
            map.put("children", listTemp);
            total.add(map);
        }
        return total;
    }
}
